import { useAppSelector } from '@/store';
import {
  delUserChannelAction,
  getAllChannelAction,
  getUserChannelAction,
  setChannelIdAction,
  setUserChannelAction,
} from '@/store/action/channels';
import { Popup, Tabs, Toast } from 'antd-mobile';
import React, { useEffect, useState } from 'react';
import { useDispatch } from 'react-redux';
import Icon from '../Icon';
import styles from './index.module.scss';
import { differenceBy } from 'lodash';
import { channelsType } from '@/store/reducer/channels';
import { useHistory } from 'react-router-dom';

export default function Channel() {
  const history = useHistory();
  const dispatch = useDispatch();
  useEffect(() => {
    dispatch(getAllChannelAction());
    dispatch(getUserChannelAction());
    return () => {};
  }, [dispatch]);
  const { userList, alllist, channel_id } = useAppSelector(
    (state) => state.channel
  );
  //   剩余未选择的列表
  const resultlist = differenceBy(alllist, userList, (item) => {
    return item.id;
  });
  // 控制选择列表的弹窗
  const [visible, setVisible] = useState(false);
  const [editable, seteditable] = useState(false);
  // const [activeId, setactiveId] = useState('0');
  /** 添加频道 */
  const hanelSetChannel = async (item: channelsType) => {
    console.log('item  ----->  ', item);
    // 3. 改造调用action的传参
    await dispatch(setUserChannelAction(item));
    Toast.show({ content: '添加成功' });
  };
  const handleDelChannel = async (id: number) => {
    // 4. 最少5个
    if (userList.length <= 5) return Toast.show({ content: '至少保留5个' });

    // 5. 推荐不能删除
    if (id === 0) return Toast.show({ content: '不可删除推荐频道' });
    // 3. 改造调用action的传参
    await dispatch(delUserChannelAction(id));
    Toast.show({ content: '删除成功' });
  };
  const handleClose = () => {
    setVisible(false);
    seteditable(false);
  };
  return (
    <div className={styles.root}>
      <Tabs
        defaultActiveKey={channel_id + ''}
        onChange={(key) => dispatch(setChannelIdAction(Number(key)))}
        className="tabs"
      >
        {userList.map((item) => {
          return <Tabs.Tab title={item.name} key={item.id} />;
        })}
      </Tabs>
      <div className="right">
        <Icon type="iconbtn_search" onClick={() => history.push('/search')} />
        <Icon type="iconbtn_channel" onClick={() => setVisible(true)} />
      </div>
      <Popup
        visible={visible}
        position="left"
        bodyClassName={styles.channelPopup}
      >
        {/* 顶部栏：带关闭按钮 */}
        <div className="channel-header">
          <Icon type="iconbtn_channel_close" onClick={() => handleClose()} />
        </div>

        {/* 频道列表 */}
        <div className="channel-content">
          {/* 当前已选择的频道列表 */}
          <div className="channel-item edit">
            <div className="channel-item-header">
              <span className="channel-item-title">我的频道</span>
              <span className="channel-item-title-extra">点击删除频道</span>
              <span
                className="channel-item-edit"
                onClick={() => seteditable(!editable)}
              >
                {editable ? '完成' : '编辑'}
              </span>
            </div>

            <div className="channel-list">
              {userList.map((item) => {
                return (
                  <span className="channel-list-item" key={item.id}>
                    {item.name}
                    {/* 3.让关闭Icon由editable决定是否显示 */}
                    {editable && (
                      <Icon
                        onClick={() => handleDelChannel(item.id)}
                        type="iconbtn_tag_close"
                      />
                    )}
                  </span>
                );
              })}
            </div>
          </div>

          {/* 推荐的频道列表 */}
          <div className="channel-item">
            <div className="channel-item-header">
              <span className="channel-item-title">频道推荐</span>
              <span className="channel-item-title-extra">点击添加频道</span>
            </div>
            <div className="channel-list">
              {resultlist.map((item) => {
                return (
                  <span
                    key={item.id}
                    className="channel-list-item"
                    onClick={() => hanelSetChannel(item)}
                  >
                    + {item.name}
                  </span>
                );
              })}
            </div>
          </div>
        </div>
      </Popup>
    </div>
  );
}
